package com.liqiye.paymanager.util;

import com.liqiye.paymanager.config.PaymentProperties;
import com.paypal.core.PayPalEnvironment;
import com.paypal.core.PayPalHttpClient;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class PayPalClientUtil {

    @Getter
    private PayPalHttpClient client;

    public PayPalClientUtil(PaymentProperties config) {
        PaymentProperties.PayPal paypal = config.getPaypal();
        String clientId = paypal.getClientId();
        String clientSecret = paypal.getClientSecret();
        String mode = paypal.getMode();

        PayPalEnvironment env = "live".equalsIgnoreCase(mode)
            ? new PayPalEnvironment.Live(clientId, clientSecret)
            : new PayPalEnvironment.Sandbox(clientId, clientSecret);
        this.client = new PayPalHttpClient(env);
    }
}
